package com.mlnx.work.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.work.dao.mysql.UserRoleMapper;
import com.mlnx.work.entity.UserRole;
import com.mlnx.work.service.UserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2020-12-28 17:09:55
 */
@Service
public class UserRoleServiceIml extends BaseService<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    @Override
    public void setBaseMapper(UserRoleMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Transactional
    @Override
    public void changeUserRole(Integer userId, Integer[] roleIds) {

        remove(new QueryWrapper<UserRole>().eq("user_id", userId));

        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0; i < roleIds.length; i++) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleIds[i]);
            userRoles.add(userRole);
        }

        saveBatch(UserRole.class, userRoles);
    }
}

